package com.jewaton.authmanage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.jewaton.authmanage.base.BaseController;
import com.jewaton.authmanage.pojo.UserInfo;
import com.jewaton.authmanage.service.IUserInfoService;
import com.jewaton.authmanage.tools.Page;

/**
 * 
 * 用户信息管理controller
 * @author magiczeng
 *
 */
@Controller
@RequestMapping(value = {"/user"})
@SessionAttributes("alertMsg")
public class UserInfoContorller extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserInfoContorller.class);
	
	@Autowired
	private IUserInfoService userInfoService;
	
	/**
	 * 查找用户列表
	 * @param user     列表查询参数，对象映射
	 * @param model  Spring的上下文属性
	 * @return
	 */
	@RequestMapping(value = {"/findUsers"})
	public String findUsers(@RequestParam(required=false) Page page, 
			@RequestParam(required=false) UserInfo user, Model model){
		page = userInfoService.findUsers(page, user);
		model.addAttribute("page", page);
		return "user/list";
	}
	
	/**
	 * 查看用户信息
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/viewInfo/{userid}"})
	public String viewInfo(@PathVariable(value="userid") Long userid, 
			Model model){
		UserInfo u = userInfoService.findUserById(userid);
		model.addAttribute("userinfo", u);
		return "user/viewInfo";
	}
	
	/**
	 * 进入新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/userInfoInput"})
	public String userInfoAdd(Model model){
			model.addAttribute("userinfo", new UserInfo("","",null));
		return "user/userInfoInput";
	}
	
	/**
	 * 进入修改页面
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/userInfoInput/{userid}"})
	public String userInfoInput(@PathVariable(value="userid") Long userid, 
			Model model){
		UserInfo u = userInfoService.findUserById(userid);
		if(u==null)
			return "redirect:findUsers";
		model.addAttribute("userinfo", u);
		return "user/userInfoInput";
	}
	
	/**
	 * 保存用户信息，新增修改共用
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/saveInfo"},method=RequestMethod.POST)
	public String saveInfo(final UserInfo userinfo,  final BindingResult bindingResult,
			Model model){
		if (bindingResult.hasErrors()) {
			Long userid = userinfo.getId();
	        return "userInfoInput"+((userid==null||userid==0)?"":"/"+userid);
	    }
		try {
			logger.info("开始保存用户信息："+userinfo.getId()+","+userinfo.getUsername()+","+userinfo.getRealname());
			userInfoService.saveUserInfo(userinfo);
		} catch (Exception e) {
			logger.error("保存用户信息时出错："+e.toString());
		}
		
		return "redirect:/user/findUsers";
	}
	
	/**
	 * 进入修改密码页面
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/modMyPassWord"})
	public String modMyPassWord(){
		return "user/modMyPassWord"; 
	}
	
	/**
	 * 进入修改密码页面
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/saveMyPassWord"},method=RequestMethod.POST)
	public String saveMyPassWord(@RequestParam String oldPassword, 
			@RequestParam String newPassword,Model model){
		UserInfo userInfo = super.getSessionUser();
		newPassword=newPassword.trim();
		oldPassword=oldPassword.trim();
		/*boolean succ = */
		userInfoService.updateUserPassword(userInfo, oldPassword, newPassword);
		//model.addAttribute("alertMsg", succ);
		return "redirect:/user/modMyPassWord"; 
	}
	
	/**
	 * 进入修改页面
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"/disableUser/{userid}"})
	public String disableUser(@PathVariable(value="userid") Long userid, @RequestParam(required=false) String enabled,
			Model model){
		/*boolean succ = */
		userInfoService.disableUser(userid, enabled);
		return "redirect:/user/findUsers";
	}

}
